//
//  NetworkingManager.swift
//  WeatherReport
//
//  Created by AndreaArlex on 16/1/30.
//  Copyright © 2016年 AndreaArlex. All rights reserved.
//

import UIKit
import Alamofire
import ObjectMapper
class NetworkingManager: NSObject {

    /**
     单例
     
     - returns: NetworkingManager对象
     */
    class func manager() ->NetworkingManager {
    
        struct Static {
        
            static var onceToken : dispatch_once_t = 0
            static var instance : NetworkingManager? = nil
        }
        
        dispatch_once(&Static.onceToken) { () -> Void in
            
            Static.instance = NetworkingManager()
        }
        
        return Static.instance!
    }
    
    
    /**
     post请求
     
     - parameter patch:          URL路径
     - parameter param:          请求参数
     - parameter successedBlock: 成功的处理
     - parameter failedBlock:    失败的处理
     */
    func postRequest(patch:NSString,
                     param:NSDictionary,
            successedBlock:(message:NSString, result:WeatherReportModel) ->Void,
               failedBlock:(error:NSError) ->Void) ->Void {
    
                Alamofire.request(.POST, patch as String, parameters: param as? [String : AnyObject], encoding: .URL, headers: nil).responseJSON {
                
                    response in
                    
                    NSLog("%@", response.result.value!.description)
                    
                    let value : NSArray = (response.result.value as! NSDictionary).objectForKey("HeWeather data service 3.0") as! NSArray
                    let model = Mapper<WeatherReportModel>().map(value[0])
                    
                    if (value[0] as! NSDictionary).objectForKey("status")!.isEqualToString("ok") {
                    
                        successedBlock(message: "", result: model! as WeatherReportModel)
                        
                    }else{
                    
                        failedBlock(error: NSError(domain: "网络失败", code: -1, userInfo: nil))
                    }
                    
                }
    }
}
